<template>
    <div class="layout">
        <div class="pre">
            <div class="title-box">客户数趋势</div>
            <div class="pre_title">
                <div class="btn_left" style="margin-bottom: 20px" @click="change($event, 1)">
                    <div :id="1" :class="['btn_item', currentIndex == 1 ? 'active' : '']">总客户数</div>
                    <div :id="2" :class="['btn_item', currentIndex == 2 ? 'active' : '']">流失客户数</div>
                </div>
            </div>
            <div v-show="!isNoMessage1" id="echarts" ref="myRef_one" class="min"></div>
            <div v-show="isNoMessage1" class="no_data">
                <img class="no_img" src="@/assets/noData.png" alt="" />
                <div class="emty_title">暂无数据</div>
            </div>
        </div>
        <div class="pre" style="padding-top: 90px">
            <div class="title-box">客户数趋势</div>
            <div class="pre_title">
                <div class="btn_left" style="margin-bottom: 20px" @click="change($event, 2)">
                    <div :id="1" :class="['btn_item', currentIndexTwo == 1 ? 'active' : '']">总客户数</div>
                    <div :id="2" :class="['btn_item', currentIndexTwo == 2 ? 'active' : '']">流失客户数</div>
                </div>
            </div>
            <div v-show="!isNoMessage2" id="echartstwo" ref="myRef_two" class="min"></div>
            <div v-show="isNoMessage2" class="no_data">
                <img class="no_img" src="@/assets/noData.png" alt="" />
                <div class="emty_title">暂无数据</div>
            </div>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref, inject, watch } from 'vue';
import { getDay } from '@/utils/timeCycle';
const echarts = inject('ec');
const currentIndex = ref('1');
const currentIndexTwo = ref('1');
const emit = defineEmits(['targetData']);
const myRef_one = ref(null);
const myRef_two = ref(null);
const isNoMessage1 = ref(false);
const isNoMessage2 = ref(false);
const props = defineProps(['completionDatathree', 'deliveryDatathree']);
const tableData = ref(null);
const params = reactive({});

const getChartTwo = (data: any) => {
    if (data.length == 0) {
        isNoMessage1.value = true;
        return;
    } else {
        isNoMessage1.value = false;
    }
    const myChart_one = echarts.init(myRef_two.value);
    myChart_one.setOption({
        title: {
            text: currentIndexTwo.value == 1 ? '总客户数' : '流失客户数',
            x: '4%',
            textStyle: {
                color: '#fff',
                fontSize: '22'
            },
            subtextStyle: {
                color: '#90979c',
                fontSize: '16'
            }
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow',
                textStyle: {
                    color: '#fff'
                }
            }
        },
        grid: {
            borderWidth: 0,
            top: 110,
            bottom: 95,
            textStyle: {
                color: '#fff'
            }
        },
        legend: {
            x: '4%',
            top: '8%',
            textStyle: {
                color: '#90979c'
            },
            data: [currentIndexTwo.value == 1 ? '总客户数' : '流失客户数']
        },

        calculable: true,
        xAxis: [
            {
                type: 'category',
                axisLine: {
                    lineStyle: {
                        color: '#90979c'
                    }
                },
                splitLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                splitArea: {
                    show: false
                },
                axisLabel: {
                    interval: 0
                },
                data: data.map((it: any) => {
                    return it.store_name;
                })
            }
        ],
        yAxis: [
            {
                type: 'value',
                splitLine: {
                    show: false
                },
                axisLine: {
                    lineStyle: {
                        color: '#90979c'
                    }
                },
                axisTick: {
                    show: false
                },
                axisLabel: {
                    interval: 0
                },
                splitArea: {
                    show: false
                }
            }
        ],
        dataZoom: [
            {
                show: true,
                height: 30,
                xAxisIndex: [0],
                bottom: 30,
                start: 10,
                end: 80,
                handleIcon:
                    'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
                handleSize: '110%',
                handleStyle: {
                    color: '#d3dee5'
                },
                textStyle: {
                    color: '#fff'
                },
                borderColor: '#90979c'
            },
            {
                type: 'inside',
                show: true,
                height: 15,
                start: 1,
                end: 35
            }
        ],
        series: [
            {
                name: currentIndexTwo.value == 1 ? '总客户数' : '流失客户数',
                type: 'bar',
                stack: '总量',
                barMaxWidth: 35,
                barGap: '10%',
                itemStyle: {
                    normal: {
                        color: '#FF6B00',
                        label: {
                            show: true,
                            textStyle: {
                                color: '#fff'
                            },
                            position: 'inside',
                            formatter: function (p) {
                                return p.value > 0 ? p.value : '';
                            }
                        }
                    }
                },
                data: data.map((it: any) => {
                    return it[currentIndexTwo.value == 1 ? 'cust_num' : 'lose_cust_num'];
                })
            }
        ]
    });
};
const getChart = (data: any) => {
    if (data.length == 0) {
        isNoMessage2.value = true;
        return;
    } else {
        isNoMessage2.value = false;
    }
    const myChart_two = echarts.init(myRef_one.value);
    myChart_two.setOption({
        title: {
            text: currentIndex.value == 1 ? '总客户数' : '流失客户数'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#FF6B00'
                }
            }
        },
        legend: {
            data: [currentIndex.value == 1 ? '总客户数' : '流失客户数']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false, //坐标轴两边留白
            data: data.map((it: any) => {
                return it.date;
            }),
            axisLabel: {
                //坐标轴刻度标签的相关设置。
                interval: 0, //设置为 1，表示『隔一个标签显示一个标签』
                //	margin:15,
                textStyle: {
                    color: '#1B253A',
                    fontStyle: 'normal',
                    fontFamily: '微软雅黑',
                    fontSize: 12
                },
                formatter: function (params) {
                    let newParamsName = '';
                    const paramsNameNumber = params.length;
                    const provideNumber = 4; //一行显示几个字
                    const rowNumber = Math.ceil(paramsNameNumber / provideNumber);
                    if (paramsNameNumber > provideNumber) {
                        for (let p = 0; p < rowNumber; p++) {
                            let tempStr = '';
                            const start = p * provideNumber;
                            const end = start + provideNumber;
                            if (p == rowNumber - 1) {
                                tempStr = params.substring(start, paramsNameNumber);
                            } else {
                                tempStr = params.substring(start, end) + '\n';
                            }
                            newParamsName += tempStr;
                        }
                    } else {
                        newParamsName = params;
                    }
                    return newParamsName;
                }
                //rotate:50,
            },
            axisTick: {
                //坐标轴刻度相关设置。
                show: false
            },
            axisLine: {
                //坐标轴轴线相关设置
                lineStyle: {
                    color: '#E5E9ED'
                    // opacity:0.2
                }
            },
            splitLine: {
                //坐标轴在 grid 区域中的分隔线。
                show: true,
                lineStyle: {
                    color: '#E5E9ED'
                    // 	opacity:0.1
                }
            }
        },
        yAxis: [
            {
                type: 'value',
                splitNumber: 5,
                axisLabel: {
                    textStyle: {
                        color: '#a8aab0',
                        fontStyle: 'normal',
                        fontFamily: '微软雅黑',
                        fontSize: 12
                    }
                },
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                splitLine: {
                    show: true,
                    lineStyle: {
                        color: '#E5E9ED'
                        // 	opacity:0.1
                    }
                }
            }
        ],
        series: [
            {
                name: currentIndex.value == 1 ? '总客户数' : '流失客户数',
                type: 'line',
                itemStyle: {
                    normal: {
                        color: '#3A84FF',
                        lineStyle: {
                            color: '#3A84FF',
                            width: 1
                        },
                        areaStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0.5, 0, 0, [
                                {
                                    offset: 0,
                                    color: '#FFC69E'
                                },
                                {
                                    offset: 1,
                                    color: '#FF6B00'
                                }
                            ])
                        }
                    }
                },
                data: data.map((it: any) => {
                    return it[currentIndex.value == 1 ? 'cust_num' : 'lose_cust_num'];
                })
            }
        ]
    });
};

const datatable = (data: any) => {
    tableData.value = data;
};
// 切换浏览的数据
const change = (event: any, type: number) => {
    if (type == 1) {
        currentIndex.value = event.target.id;
        getChart(props.completionDatathree);
    } else {
        currentIndexTwo.value = event.target.id;
        getChartTwo(props.deliveryDatathree);
    }
};

watch(
    () => props.completionDatathree,
    val => {
        getChart(val);
    }
);
watch(
    () => props.deliveryDatathree,
    val => {
        getChartTwo(val);
    }
);
defineExpose({ change }); //像父组件抛出方法
</script>
<style lang="scss" scoped>
.layout {
    box-sizing: border-box;
    width: 100%;
    margin-top: 16px;
    padding-left: 30px;

    // display: flex;
    // justify-content: space-between;
    .title-box {
        font-size: 16px;
        font-family: STHeitiSC-Medium, STHeitiSC;
        font-weight: 500;
        color: #33302d;
        padding-bottom: 25px;
        border-bottom: solid 1px #eaeaea;
        margin-bottom: 19px;
    }

    .pre {
        width: 100%;
        height: 750px;
        box-sizing: border-box;
        background: #fff;

        // border: 1px solid #EAEAEA;
        .pre_title {
            padding: 0;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border: none;

            .btn_left {
                display: flex;
                align-items: center;

                .btn_item {
                    padding: 10px 18px;
                    background: #fff;
                    font-size: 16px;
                    line-height: 16px;
                    font-weight: 500;
                    color: #ff6b00;
                    border: 1px solid #ff6b00;
                    cursor: pointer;
                }

                .active {
                    background: #ff6b00;
                    color: #fff;
                }
            }
        }

        &_title {
            padding: 20px 30px;
            border-bottom: 1px solid #eaeaea;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;

            .export {
                display: inline-block;
                width: 64px;
                height: 36px;
                text-align: center;
                line-height: 36px;
                background: #fff;
                border-radius: 4px;
                border: 1px solid #eaeaea;
                cursor: pointer;
            }
        }

        .title-text {
            line-height: 38px;
        }

        .min {
            height: 100%;
            background: #fff;
        }
    }

    .no_data {
        height: 500px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;

        .no_img {
            margin-left: 35px;
        }
    }
}

.el-form-item {
    margin-bottom: 0;
}
</style>
